/**
 * Created by myself on 2016/6/30.
 */

//修改对象
function updateObj(objid,datarow)
{

    bootbox= bootbox_global;

    var obj = grid.date_list[datarow];




    var alertarray = {
        form: {api: "/mall/updateMaterial", name: "alertform", id: "alertform"},
        inputs: [

            {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name",value:obj.name},
            {title: "图片", type: "img", placeholder: "", verify: "empty", name: "pic_url", id: "pic_url",src:obj.pic_url},
            {title: "种类", type: "select", id: "roles", form_name: "type_id", copyid: "typeselect",selectvalue:obj.type_id},
        ]

    }

    bootbox.dialog({
        title: "修改信息",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });

}

//删除
function deleteObj(objid)
{

    bootbox= bootbox_global;

    bootbox_global.dialog({
        message: "确定要删除吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/mall/deleteMaterial", "obj_id="+objid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}



var bootbox_global;
var grid;
define([
    "jquery",
    "IO",
    "bootbox",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
], function ($, IO, bootbox) {

    bootbox_global = bootbox;

    $('#fileupload').fileupload({
        url: '/uploadmallcategory',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {

        var imgid = $(e.target).attr('imgid');
        document.getElementById(imgid).src = laoding_gif;

    }).on('fileuploadprocessalways', function (e, data) {
        //console.log("fileuploadprocessalways",data);
    }).on('fileuploadprogressall', function (e, data) {
        //console.log("fileuploadprogressall",data);
    }).on('fileuploaddone', function (e, data) {

        console.log("upyuntype " + $('#fileupload').attr("upyuntype"));
        var imgid = $(e.target).attr('imgid');
        document.getElementById(imgid).src = data._response.result.url;
    });


    $(function () {
        var columns_select ={name_dataIndex :'name',value_dataIndex :'type_id'};
                 var  grid_select = new Select_Grid("/mall/getMaterialTypeList",$("#typeselect"),columns_select,"rows=100000");

                    grid_select.removeflag = 1;
                    grid_select.Init(function(){

                             dataInit();

                         });

        //每列数据列性
        var columns = [

            {
                title: '编号', dataIndex: 'ID', dataSource: function (data, datarow, gridobj, current_column) {

                return "<span>" + parseInt((gridobj.current_page - 1) * gridobj.page_rows + parseInt(datarow) + 1) + "</span>";
                }
            },
            {title: '名称', dataIndex: 'name',},
            {title: '所属种类', dataIndex: 'type_name',},
            {
                title: '图片', dataIndex: 'avatar', dataSource: function (data, datarow, gridobj, current_column) {

                var avatar = data[datarow].pic_url;

                if (avatar.length == 0)
                    return "";
                else
                    return "<img class='girdimg img-thumbnail' onclick=\"viewUrl('" + avatar + "')\" src='" + avatar + "'/>"


                }
            },
            {
                title: '操作', dataIndex: 'ID', dataSource: function (data, datarow, gridobj, current_column) {

                var html = "<div class='div_btn'><button onclick=\"updateObj(" + data[datarow].material_id + "," + datarow + ")\" class='btn btn-info'>修改</button> <button onclick=\"deleteObj(" + data[datarow].material_id + ")\"  class='btn btn-danger'>删除</button>";


                html = html + "</div>"

                return html;
            }
            },



        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/mall/getMaterialListPaging", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param = "type_id=" + $("#typeselect").val();

            return param;

        }

        //dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });
    });


    //新增
    $("#btn_add").bind("click", function () {


        var alertarray = {
            form: {api: "/mall/addMaterial", name: "alertform", id: "alertform"},
            inputs: [

                {title: "名称", type: "text", placeholder: "", verify: "empty", name: "name", id: "name"},
                {title: "图片", type: "img", placeholder: "", verify: "empty", name: "pic_url", id: "pic_url",src:""},
                {title: "种类", type: "select", id: "roles", form_name: "type_id", copyid: "typeselect"},

            ]

        }
        bootbox.dialog({
            title: "新增",
            message: generateAlertForm(alertarray),
            buttons: {
                "cancel": {
                    "label": "<i class='icon-info'></i> 取消",
                    "className": "btn-sm btn-danger",
                    "callback": function () {
                    }
                },
                "success": {
                    "label": "<i class='icon-ok'></i> 保存",
                    "className": "btn-sm btn-success",
                    "callback": function () {

                        //验证
                        if (check_form_info_all(alertarray.form.name) == false)
                            return false;

                        //发送请求
                        ajax_request(alertarray.form.name, "&parent_id=-1", function process_data(data) {


                            //操作成功
                            AlertSuccess(bootbox, "操作成功", function () {

                                grid.Init();


                            });


                        }, function process_fail(data) {

                            //操作失败
                            AlertError(bootbox, data.msg);


                        });


                    }
                }
            }
        });


    });


});